package cn.mistorange.controller;

import cn.mistorange.pojo.Result;
import cn.mistorange.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询全部类型
    @GetMapping
    public Result GetAllCategories(String type) {
        return Result.success(categoryService.getAllCategories(type));
    }

    //id查询
    @GetMapping("/{id}")
    public Result getCategoryById(@PathVariable Integer id) {
        return Result.success(categoryService.getCategoryById(id));
    }

    //添加类型
    @PostMapping
    public Result addCategory(String name, String type) {
        categoryService.addCategory(name, type);
        return Result.success();
    }

    //删除类型
    @DeleteMapping("/{id}")
    public Result deleteCategory(@PathVariable Integer id){
        categoryService.deleteCategory(id);
        return Result.success();
    }

    //修改类型
    @PutMapping
    public Result updateCategory(Integer id, String name, String type){
        categoryService.updateCategory(id, name, type);
        return Result.success();
    }
}
